package com.bezdelnick.forum.service;

import com.bezdelnick.forum.dao.*;
import com.bezdelnick.forum.domain.*;
import com.bezdelnick.forum.mail.MailSendManager;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;

import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: Bezdelnick
 * Date: 21.07.12
 * Time: 12:52
 * To change this template use File | Settings | File Templates.
 */
public class ForumService {
    private static final Logger LOGGER = LoggerFactory.getLogger(ForumService.class);

    public enum MODE {USER, ADMIN}

    private IAnswerDao answerDao;
    private IAppUserDao appUserDao;
    private IQuestionDao questionDao;
    private ITypeDao typeDao;
    private ISubtypeDao subtypeDao;
    private IRoleDao roleDao;
    private MailSendManager mailSendManager;

    public IAnswerDao getAnswerDao() {
        return answerDao;
    }

    public void setAnswerDao(IAnswerDao answerDao) {
        this.answerDao = answerDao;
    }

    public IAppUserDao getAppUserDao() {
        return appUserDao;
    }

    public void setAppUserDao(IAppUserDao appUserDao) {
        this.appUserDao = appUserDao;
    }

    public IQuestionDao getQuestionDao() {
        return questionDao;
    }

    public void setQuestionDao(IQuestionDao questionDao) {
        this.questionDao = questionDao;
    }

    public ITypeDao getTypeDao() {
        return typeDao;
    }

    public void setTypeDao(ITypeDao typeDao) {
        this.typeDao = typeDao;
    }

    public ISubtypeDao getSubtypeDao() {
        return subtypeDao;
    }

    public void setSubtypeDao(ISubtypeDao subtypeDao) {
        this.subtypeDao = subtypeDao;
    }

    public IRoleDao getRoleDao() {
        return roleDao;
    }

    public void setRoleDao(IRoleDao roleDao) {
        this.roleDao = roleDao;
    }

    public MailSendManager getMailSendManager() {
        return mailSendManager;
    }

    public void setMailSendManager(MailSendManager mailSendManager) {
        this.mailSendManager = mailSendManager;
    }

    public AppUser getCurrentUser() {
        User currentUser = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        AppUser appUser = getAppUserDao().getUserByName(currentUser.getUsername());
        return appUser;
    }

    public List<String> loadTypesNames() {
        List<String> typesNames = new ArrayList<String>();
        List<Type> types = this.getTypeDao().loadTypes();

        for (Type type : types) {
            typesNames.add(type.getName());
        }

        return typesNames;
    }

    public List<String> loadSubtypesNames() {
        List<String> subtypesNames = new ArrayList<String>();
        List<Subtype> subtypes = this.getSubtypeDao().loadSubtypes();

        for (Subtype subtype : subtypes) {
            subtypesNames.add(subtype.getName());
        }

        return subtypesNames;
    }

    public List<String> loadSubtypesNamesByTypeId(long id) {
        List<String> subtypesNames = new ArrayList<String>();
        List<Subtype> subtypes = this.getSubtypeDao().loadSubtypesByTypeId(id);

        for (Subtype subtype : subtypes) {
            subtypesNames.add(subtype.getName());
        }

        return subtypesNames;
    }

    public boolean hasAdminAuthority() {
        boolean access = false;
        try {
            User currentUser = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            Collection<GrantedAuthority> authorities = currentUser.getAuthorities();
            for (GrantedAuthority authority : authorities) {
                if (authority.getAuthority().equals("ROLE_ADMIN")) {
                    access = true;
                    break;
                }
            }
        } catch (Exception ex) {
            LOGGER.error(ex.getMessage());
            return false;
        }

        return access;
    }
}
